package com.ad_hccm.service.impl.Appendix;

import com.ad_hccm.dao.Appendix.CompletionStatusDao;
import com.ad_hccm.domain.Appendix.CompletionStatus;
import com.ad_hccm.domain.Appendix.Eliminate;
import com.ad_hccm.domain.Appendix.Exit;
import com.ad_hccm.domain.Appendix.SuspendExperiment;
import com.ad_hccm.service.Appendix.CompletionStatusService;
import com.ad_hccm.service.Appendix.EliminateService;
import com.ad_hccm.service.Appendix.ExitService;
import com.ad_hccm.service.Appendix.SuspendExperimentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * TODO 临床试验完成情况
 */
@Service
public class CompletionStatusServiceImpl implements CompletionStatusService {
    @Autowired
    private CompletionStatusDao dao;
    @Autowired
    private EliminateService eliminateService;
    @Autowired
    private ExitService exitService;
    @Autowired
    private SuspendExperimentService suspendExperimentService;

    @Override
    public boolean save(CompletionStatus domain) {
        return dao.save(domain);
    }

    @Override
    public boolean update(CompletionStatus domain) {
        if(domain.getType()==1){
            Eliminate e = new Eliminate();
            e.setId(domain.getId());
            if(eliminateService.getById(domain.getId())==null)eliminateService.save(e);
            if(exitService.getById(domain.getId())!=null)exitService.delete(domain.getId());
            if(suspendExperimentService.getById(domain.getId())!=null)suspendExperimentService.delete(domain.getId());
        }else if(domain.getType() == 2){
            Exit e = new Exit();
            e.setId(domain.getId());
            if(exitService.getById(domain.getId())==null)exitService.save(e);
            if(suspendExperimentService.getById(domain.getId())!=null)suspendExperimentService.delete(domain.getId());
            if(eliminateService.getById(domain.getId())!=null)eliminateService.delete(domain.getId());
        }else if(domain.getType() == 3){
            SuspendExperiment s = new SuspendExperiment();
            s.setId(domain.getId());
            if(suspendExperimentService.getById(domain.getId())==null)suspendExperimentService.save(s);
            if(exitService.getById(domain.getId())!=null)exitService.delete(domain.getId());
            if(eliminateService.getById(domain.getId())!=null)eliminateService.delete(domain.getId());
        }
        return dao.update(domain);
    }

    @Override
    public CompletionStatus getById(String id) {
        return dao.getById(id);
    }

    @Override
    public List<CompletionStatus> getAll() {
        return dao.getAll();
    }

    @Override
    public boolean delete(String id) {
        //把子表数据一起删，不然删不了
        if(eliminateService.getById(id)!=null)eliminateService.delete(id);
        if(exitService.getById(id)!=null)exitService.delete(id);
        if(suspendExperimentService.getById(id)!=null)suspendExperimentService.delete(id);
        return dao.delete(id);
    }
}
